﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SisVet.ModelBindings;

namespace SisVet.ServicosModelo
{
    public class ServicosDAL
    {
        SisVetEntities contexto = new SisVetEntities();

        public List<Servicos> ListarServicos()
        {
            var a = from p in contexto.Servicos
                    select p;

            return a.ToList();
        }

        public void CadastrarServico(string NomeServico, double Quantidade, double Valor)
        {
            Servicos servicos = new Servicos() { NmServico = NomeServico, Quantidade = Quantidade, Valor = Valor };
            contexto.Servicos.Add(servicos); // importante, pois aqui ocorre o comando INSERT
            contexto.SaveChanges(); // aqui concretizo o insert, inserindo no DB
        }

        public void AlterarServico(int CodServico, string NomeServico, double Quantidade, double Valor)
        {
            var servi = (from p in contexto.Servicos
                         where p.CdServico == CodServico
                         select p).FirstOrDefault();

            servi.NmServico = NomeServico;
            servi.Quantidade = Quantidade;
            servi.Valor = Valor;
            contexto.Entry(servi).State = System.Data.EntityState.Modified; // aqui que é o comando UPDATE
            contexto.SaveChanges(); // aqui concretizo o update, salvando no DB
        }

        public void ExcluirServico(int CodServico)
        {
            var servicos = (from p in contexto.Servicos
                            where p.CdServico == CodServico
                            select p).FirstOrDefault();

            contexto.Servicos.Remove(servicos);
            contexto.SaveChanges();
        }

        public List<Servicos> PesquisarPorNome(String pesquisa, int indice)
        {
            var servicos = (from p in contexto.Servicos
                            select p);
            int codigo;

            if (indice == 1)
                servicos = servicos.Where(p => p.NmServico.Contains(pesquisa));
            else
            {
                codigo = Convert.ToInt32(pesquisa);
                servicos = servicos.Where(p => p.CdServico == codigo);
            }


            return servicos.ToList();
        }

        public Servicos PesquisaPorCodigo(int codigo)
        {
            var servico = (from p in contexto.Servicos
                           where p.CdServico == codigo
                           select p);
            return servico.FirstOrDefault();
        }

    }
}
